/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

public class Server {
    
    private final int port;
    
    private final static int MAX_NUMBER = 94;
    
    private final AtomicLong curNum = new AtomicLong(0);
    
    private final ExecutorService exec = Executors.newCachedThreadPool();
    
    public Server(int port) {
        this.port = port;
    }
    
    public static void main(String[] args) {
        Server server = new Server(6666);
        server.start();
    }
    
    public void start() {
        Thread daemon = new Thread(new Status(exec));
        daemon.setDaemon(true);
        daemon.start();
        
        try {
            ServerSocket ss = new ServerSocket(port);
            while(true) {
                Socket s = ss.accept();
                exec.execute(new ClientController(s, this));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public long next() {
        long i;
        
        while (true) {
            long current = curNum.get();
            long next = (current + 1) % MAX_NUMBER;
            if (curNum.compareAndSet(current, next)) {
                i = next;
                break;
            }
        }
        
        long num = (long) ((Math.pow((1 + Math.sqrt(5))/2, i) -  Math.pow((1 - Math.sqrt(5))/2, i)) / Math.sqrt(5) );
        long res = num;
        return res;
    }
}